package com.yingside.gui;

import javax.swing.*;

public class TestJTableFrame extends JFrame {

    private JTable table;
    private JScrollPane scrollPane;
    public TestJTableFrame() {
        this.setTitle("JTable窗口");
        this.setBounds(300, 300, 400, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //一维数组作为表头
        String [] header = {"序号","书名","作者","价格"};
        //二维数组作为数据显示
        Object [][] objs = {
                {"1","斗破苍穹1","天蚕土豆1","200"},
                {"2","斗破苍穹2","天蚕土豆2","200"},
                {"3","斗破苍穹3","天蚕土豆3","200"},
                {"4","斗破苍穹4","天蚕土豆4","200"}
        };

        table = new JTable(objs,header);
        //默认要求表格要放在一个JScrollPane中，不然不会显示表头

        scrollPane = new JScrollPane(table);

        this.add(scrollPane);

    }

    public static void main(String[] args) {
        TestJTableFrame tableFrame = new TestJTableFrame();
        tableFrame.setVisible(true);
    }
}
